package com.dd.stack.redis.service.inner.impl;

import com.dd.stack.common.util.Result;
import com.dd.stack.redis.config.redisConfig.constants.ChannelNameConstants;
import com.dd.stack.redis.pojo.BO.OrderBO;
import com.dd.stack.redis.pojo.req.OrderReq;
import com.google.gson.Gson;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * @Author liuxianmeng
 * @CreateTime 2025/1/31 11:49
 * @Description OrderServiceImpl
 */
@Service
public class OrderServiceImpl {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public Result create(OrderReq req) {
        // pojo转换
        OrderBO BO = new OrderBO();
        BeanUtils.copyProperties(req, BO);
        BO.setItems(req.getItems());
        // 生成订单ID
        String orderId = UUID.randomUUID().toString();
        BO.setOrderId(orderId);
        // 将订单信息转换为 JSON 字符串
        String orderJson = new Gson().toJson(BO);
        // 发布订单信息到Redis频道 后续的操作交给Redis
        stringRedisTemplate.convertAndSend(ChannelNameConstants.CHANNEL_ORDER_V2, orderJson);
        return Result.success("Order created and published to channel: " + orderId);
    }
}
